﻿using QFramework;
using UnityEngine;

namespace ProjectSurvival
{
    public abstract class PowerUp:GamePlayObject
    {
        protected override Collider2D Collider { get; }
        public bool FlyToPlayer { get; set; }
        private float flyToPlayerTime = 0;
        protected abstract void Execute();
        private void Update()
        {
            if (FlyToPlayer)
            {
                if (flyToPlayerTime == 0)
                {
                    this.GetComponent<SpriteRenderer>().sortingOrder = 5;
                }

                flyToPlayerTime += Time.deltaTime;
                if (Player.Instance)
                {
                    var direction = Player.Instance.Direction2DFrom(this);
                    var distance = direction.magnitude;
                    if (flyToPlayerTime <= 0.25f)
                    {
                        transform.Translate(direction.normalized*-2*Time.deltaTime);
                    }
                    else
                    {
                        transform.Translate(direction.normalized*7*Time.deltaTime);
                    }

                    if (distance<=0.1f)
                    {
                        Execute();
                    }
                }
            }
        }
    }
}